package com.example.csupermaket.service.impl;

import com.example.csupermaket.exception.ServiceException;
import com.example.csupermaket.mapper.ProductMapper;
import com.example.csupermaket.pojo.dto.ProductAddNewDTO;
import com.example.csupermaket.pojo.dto.ProductUpdateDTO;
import com.example.csupermaket.pojo.entity.Product;
import com.example.csupermaket.pojo.vo.ProductListItemVO;
import com.example.csupermaket.pojo.vo.ProductStandardVO;
import com.example.csupermaket.service.IProductService;
import com.example.csupermaket.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Slf4j
@Service
public class ProductServiceImpl implements IProductService {

    @Value("${imagesPath}")
    private String dirPath;

    @Autowired
    private ProductMapper productMapper;

    @Override
    public void addNew(ProductAddNewDTO productAddNewDTO) {
        String pName = productAddNewDTO.getPName();


        int i = productMapper.countByName(pName);

        if (i>0){
            String message = "添加失败，商品名称被占用！";
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }

        if (productAddNewDTO.getPName()==null){
            String message = "添加失败，商品名称不能为空！";
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }

        if (productAddNewDTO.getPrice()==null){
            String message = "添加失败，商品价格不能为空！";
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }

        if (productAddNewDTO.getInventory()==null){
            String message = "添加失败，商品库存不能为空！";
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }

        if (productAddNewDTO.getCid()==null){
            String message = "添加失败，商品类别不能为空！";
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }

        if (productAddNewDTO.getUrl()==null){
            String message = "添加失败，商品图片不能为空！";
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }

        Product product = new Product();

        BeanUtils.copyProperties(productAddNewDTO,product);

        productMapper.insert(product);
    }

    @Override
    public List<ProductListItemVO> list(){

        List<ProductListItemVO> list = productMapper.list();



        return list;
    }

    @Override
    public String selectUrlById(int id) {
        String url = productMapper.selectUrlById(id);
        return url;
    }


    @Override
    public void delete(int id){
        // 调用Mapper对象的getStandardById()执行查询
        ProductStandardVO queryResult = productMapper.getStandardById(id);
        // 判断查询结果是否为null
        if (queryResult == null) {
            // 是：数据不存在，抛出异常
            String message = "删除商品失败，尝试删除的商品不存在！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        String url = productMapper.selectUrlById(id);
        new File(dirPath+url).delete();
        // 调用Mapper对象的deleteById()方法执行删除
        log.debug("即将执行删除，参数：{}", id);
        productMapper.deleteById(id);
    }


    @Override
    public void update(int id, ProductUpdateDTO productUpdateDTO){

        ProductStandardVO queryResult = productMapper.getStandardById(id);



        if (queryResult == null) {
            // 是：数据不存在，抛出异常
            String message = "修改商品失败，尝试修改的商品不存在！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }

        String name = productUpdateDTO.getPName();
        int count = productMapper.countByNameAndNotId(id,name);
        if (count>0){
            String message = "修改失败，商品名称被占用！";
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }

        Product product = new Product();
        BeanUtils.copyProperties(productUpdateDTO,product);
        product.setId(id);
        productMapper.update(product);
    }

    @Override
    public ProductStandardVO getStandardById(int id) {
        ProductStandardVO queryResult = productMapper.getStandardById(id);
        if (queryResult == null) {
            String message = "获取商品详情失败，尝试访问的数据不存在！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        return queryResult;
    }
}
